package org.example.lys.utils;

/**
 * @author caosong
 * @date 2020/4/22 下午5:59
 */
public class StringUtil {
    public static boolean isEmpty(String str) {
        return str == null || "".equals(str.trim()) || "null".equals(str.trim().toLowerCase());
    }

    public static String trim(String str) {
        String rtnStr = "";
        if (isEmpty(str)) {
            return rtnStr;
        } else {
            rtnStr = str.trim();
            return rtnStr;
        }
    }

    public static String cooperatorNameFilter(String name) {
        String head = name.substring(0, 1);
        String end = name.substring(name.length() - 1);
        String regex = "^[A-Za-z0-9]";
        if (head.matches(regex)) {
            name = name.replaceFirst(head, "");
        }
        if (end.matches(regex)) {
            name = name.substring(0, name.length() - 1);
        }
        return name;
    }

    /**
     * 构建定长的字符串
     * <p>
     * 全角空格(中文符号)
     *
     * @param str 原字符串
     * @param len 定长字符串长度
     * @return
     */
    public static String changeLengthU3000(String str, int len) {
        int size = str.length();
        if (size > len) {
            return str;
        }
        String temp = str;
        for (int i = 0; i < len - size; i++) {
            temp += "\u3000";
        }
        return temp;
    }

    /**
     * 构建定长的字符串
     * <p>
     * 半角空格(英文符号)
     *
     * @param str 原字符串
     * @param len 定长字符串长度
     * @return
     */
    public static String changeLengthU0020(String str, int len) {
        int size = str.length();
        if (size > len) {
            return str;
        }
        String temp = str;
        for (int i = 0; i < len - size; i++) {
            temp += "\u0020";
        }
        return temp;
    }
}
